package com.artqiyi.dollmachine.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseApp;
import com.artqiyi.dollmachine.common.constant.ServerConstant;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.Callback.MyStringCallback;
import com.artqiyi.dollmachine.utils.JSONUtils;
import com.artqiyi.dollmachine.utils.manager.HttpManager;
import com.artqiyi.dollmachine.utils.manager.PaymentManager;
import com.artqiyi.dollmachine.utils.manager.UserInfoManager;
import com.artqiyi.dollmachine.utils.respone.ConfigRespone;
import com.artqiyi.dollmachine.utils.respone.PaymentRespone;
import com.artqiyi.dollmachine.utils.respone.UserInfoRespone;
import com.artqiyi.dollmachine.views.activity.MainActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import timber.log.Timber;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = WXPayEntryActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_loading);
        BaseApp.mWxApi.handleIntent(getIntent(), this);
    }

	@Override
	public void onReq(BaseReq req) {
	}

	/*0 支付成功
	-1 发生错误 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
	-2 用户取消 发生场景：用户不支付了，点击取消，返回APP。*/
	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "errCode = " + resp.errCode);// 支付结果码
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			int code = resp.errCode;
			switch (code) {
				case 0:
					Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
					if (PaymentManager.getInstance().getPaymentId() != 0) {
						getQueryTrasactionID(String.valueOf(PaymentManager.getInstance().getPaymentId()));
					}
					break;
				case -1:
					Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
					finish();
					break;
				case -2:
					Toast.makeText(this, "支付取消", Toast.LENGTH_SHORT).show();
					finish();
					break;
				default:
					Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
					setResult(RESULT_OK);
					finish();
					break;
			}
		}
	}

//	//支付交易单号查询
	public void getQueryTrasactionID(String trasactionID) {
		HttpManager.getHttpRequest(ServerConstant.getQueryTrasactionID() + trasactionID, null, new MyStringCallback() {
			@Override
			public void onError(Call call, Exception e, int id) {
				Toast.makeText(WXPayEntryActivity.this, "查询交易单失败", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onResponse(String response, int id) {
				PaymentRespone paymentRespone = JSONUtils.jsonString2Bean(response, PaymentRespone.class);
				if (TextUtils.equals(paymentRespone.getData().getStatus(), "paid")) {
					Toast.makeText(WXPayEntryActivity.this, "订单支付成功", Toast.LENGTH_SHORT).show();
					getUserInfo();
				} else {
					Toast.makeText(WXPayEntryActivity.this, "订单支付失败", Toast.LENGTH_SHORT).show();
					finish();
				}
			}
		});
	}

	public void getUserInfo() {
		HttpManager.getHttpRequest(ServerConstant.getUserInfo(), null, new MyStringCallback() {
			@Override
			public void onError(Call call, Exception e, int id) {
				Timber.d("请求个人信息失败");
			}

			@Override
			public void onResponse(String response, int id) {
				UserInfoRespone userInfoRespone = JSONUtils.jsonString2Bean(response, UserInfoRespone.class);
				UserInfoManager.getInstance().setUserInfoBeanWithoutToken(userInfoRespone.getData());
				WXPayEntryActivity.this.finish();
			}
		});
	}

}